package com.bank;

import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import com.bank.lenovoz5.Z5PAbank;
import com.receive.SMSReceiverSend;
import com.show.api.util.StringUtils;
import com.utils.ChatClient;
import com.utils.CodeUtils;
import com.utils.Constants;
import com.utils.FileHelper;
import com.utils.LogToFile;
import com.utils.Util;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.text.DecimalFormat;
import java.util.List;

import static android.os.SystemClock.sleep;
import static com.utils.ChatClient.CCBMethodName;
import static com.utils.ChatClient.CODE_TIMER;
import static com.utils.ChatClient.C_updateBalance_W;
import static com.utils.ChatClient.ConfirmOrder;
import static com.utils.ChatClient.PAUSE;
import static com.utils.ChatClient.RUNING;
import static com.utils.ChatClient.S_paused_C;
import static com.utils.ChatClient.addWorkTime;
import static com.utils.ChatClient.getSerialNumber;
import static com.utils.ChatClient.getmap;
import static com.utils.ChatClient.list;
import static com.utils.ChatClient.mapDelete;
import static com.utils.ChatClient.setDevicesLog;
import static com.utils.ChatClient.user;
import static com.utils.Constants.orderBankName;
import static com.utils.UIUtils.showLog;

/**
 * 描述：平安银行自动化脚本
 * 时间：Created by Administrator on 2017/4/13.
 */
@RunWith(AndroidJUnit4.class)
public class PAbank extends Bank {
    private static String tableType = "abc";
    public static String sumMoney = "-1";
    private int[] tableAbc123;
    private int[][] xy_123;
    private int[][] xy_abc;
    private int[][] xy_num_m;

    // 默认分辨率 1080*1920
    // 键盘字母和数字切换
    private static int[] tableAbc123_2016 = {120, 1930};
    private static int[] tableAbc123_2102 = {120, 2015};
    private static int[][] xy_123_2016 = new int[][]{
            {170, 1450}, {530, 1450}, {890, 1450},
            {170, 1610}, {530, 1610}, {890, 1610},
            {170, 1770}, {530, 1770}, {890, 1770},
            {530, 1930}};
    private static int[][] xy_123_2102 = new int[][]{
            {170, 1530}, {530, 1530}, {890, 1530},
            {170, 1700}, {530, 1700}, {890, 1700},
            {170, 1860}, {530, 1860}, {890, 1860},
            {530, 2015}};
    private static int[][] xy_abc_2016 = new int[][]{
            {60, 1460}, {160, 1460}, {270, 1460}, {370, 1460}, {480, 1460}, {590, 1460}, {700, 1460}, {800, 1460}, {910, 1460}, {1010, 1460},
            {120, 1615}, {220, 1615}, {320, 1615}, {430, 1615}, {540, 1615}, {650, 1615}, {750, 1615}, {850, 1615}, {960, 1615},
            {220, 1775}, {320, 1775}, {430, 1775}, {540, 1775}, {650, 1775}, {750, 1775}, {850, 1775}};
    private static int[][] xy_abc_2102 = new int[][]{
            {60, 1550}, {160, 1550}, {270, 1550}, {370, 1550}, {480, 1550}, {590, 1550}, {700, 1550}, {800, 1550}, {910, 1550}, {1010, 1550},
            {120, 1700}, {220, 1700}, {320, 1700}, {430, 1700}, {540, 1700}, {650, 1700}, {750, 1700}, {850, 1700}, {960, 1700},
            {220, 1850}, {320, 1850}, {430, 1850}, {540, 1850}, {650, 1850}, {750, 1850}, {850, 1850}};
    private static int[][] xy_num_m_2016 = new int[][]{
            {170, 1450}, {540, 1450}, {900, 1450},
            {170, 1610}, {540, 1610}, {900, 1610},
            {170, 1770}, {540, 1770}, {900, 1770},
            {170, 1940}, {540, 1940}};
    private static int[][] xy_num_m_2102 = new int[][]{
            {170, 1530}, {540, 1530}, {900, 1530},
            {170, 1700}, {540, 1700}, {900, 1700},
            {170, 1860}, {540, 1860}, {900, 1860},
            {170, 1940}, {540, 1940}};

    static {
        BANK_FLAG = "PA";
        BANK_NAME = "平安口袋银行";
        smsCode = "95511";
        context = InstrumentationRegistry.getTargetContext();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        ratio = screenWidth / 720f;
        ratioH = screenHeight / 1080f;
        Constants.CODE = "";
    }

    //键盘适配
    @Override
    public void keyPhone(int screenHeight) {
        showMsg("适配键盘坐标...");
        String h = String.valueOf(screenHeight);
        switch (h) {
            case "2102":
                tableAbc123 = tableAbc123_2102;
                xy_123 = xy_123_2102;
                xy_abc = xy_abc_2102;
                xy_num_m = xy_num_m_2102;
                break;
            default:
                tableAbc123 = tableAbc123_2016;
                xy_123 = xy_123_2016;
                xy_abc = xy_abc_2016;
                xy_num_m = xy_num_m_2016;
                break;
        }
    }

    @Override
    public void keyPhone(String device) {

    }

    @Test
    public void demo() throws UiObjectNotFoundException {
        LogToFile.init(context, Constants.PROT);
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        Constants.getAccountInformation(Constants.getMethod(0));
        keyPhone(screenHeight);
        // 启动银行
        rebootAPP();
        Constants.getsTheModelAndVersionNumberAndVersionName(context, BANK_NAME);
        // 注册短信广播
        SMSReceiverSend sMSReceiverSend = new SMSReceiverSend();
        Constants.registerSMSMonitor(context, sMSReceiverSend, "95511");
        String carrier = android.os.Build.MODEL;
        showMsg("carrier===" + carrier);
        if (carrier.contains("Lenovo L78011")) {
            showMsg("进入Z5...");
            showLog("进入Z5");
            new Z5PAbank(mDevice);
        }
        info();
    }

    private void rebootAPP() throws UiObjectNotFoundException {
        Constants.scriptToStart(mDevice, BANK_NAME);
        //重新启动银行app
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_12, "检测重启超时", Constants.notReport);
            if (isOutTime) {
                mDevice.pressHome();
                LogToFile.showLog(orderBankName, "点击home键");
                Constants.tryingToReadAccountInformation(mDevice, BANK_NAME);
                //启动银行app
                while (true) {
                    UiObject x = CodeUtils.getUiObject_contentText(BANK_NAME);
                    if (x.exists()) {
                        LogToFile.showLog(orderBankName, "再次点击启动银行");
                        x.click();
                        sleep(ChatClient.overtime_3);
                        break;
                    }
                }
            }
            handleAdvertising(0);
            UiObject transfer1 = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/item_label").index(5));
            UiObject transfer2 = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/tv_first").index(0));
            if (!transfer1.exists()) {
                transfer1 = CodeUtils.getUiObject_textOrId("转账", "com.pingan.paces.ccms:id/tv_native_title");
            }
            if (transfer1.exists()) {
                showMsg("进入首页...");
                break;
            } else if (transfer2.exists()) {
                showMsg("进入首页...");
                break;
            }
        }
    }

    /**
     * @throws UiObjectNotFoundException 异常捕获
     */
    private void info() {
        sleep(ChatClient.overtime_5);
        try {
            transfer();
        } catch (UiObjectNotFoundException e) {
            showMsg(e.getMessage());
            e.printStackTrace();
        }
    }


    //检索转账：进入转账
    private void transfer() throws UiObjectNotFoundException {
        //获取页面上登录节点
        long flagTime = ChatClient.overtime_8;
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean outTime = CodeUtils.outTime(ChatClient.begintime, (int) flagTime, "提示弹窗处理完成", Constants.notReport);
            if (outTime) {
                break;
            }

            flagTime = handleAdvertising(flagTime);
            showMsg("等待广告...");
            sleep(ChatClient.overtime_1);
        }

        sleep(ChatClient.overtime_3);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            UiObject transfer1 = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/item_label").index(5));
            UiObject transfer2 = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/tv_first").index(0));
            if (!transfer1.exists()) {
                transfer1 = CodeUtils.getUiObject_textOrId("转账", "com.pingan.paces.ccms:id/tv_native_title");
            }
            if (transfer1.exists()) {
                transfer1.click();
                showMsg(">>>>>转账transfer1");
                Login();
                break;
            } else if (transfer2.exists()) {
                transfer2.click();
                showMsg(">>>>>转账transfer1");
                Login();
                break;
            }
        }
    }

    /**
     * 处理广告
     *
     * @param time
     */
    private long handleAdvertising(long time) throws UiObjectNotFoundException {
        UiObject home_dialog = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/btn_left").text("取消"));
        if (home_dialog != null && home_dialog.exists()) {
            showMsg(">>>>>弹出提示弹窗关闭");
            home_dialog.click();
            time += ChatClient.overtime_3;
        }
        UiObject home_dialog1 = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/rl_close_background"));
        if (home_dialog1 != null && home_dialog1.exists()) {
            showMsg(">>>>>弹出提示弹窗关闭1");
            home_dialog1.click();
            time += ChatClient.overtime_3;
        }
        UiObject mBT_close = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/bt_cancel").text("取消"));
        if (mBT_close.exists()) {
            showMsg(">>>>>截图弹窗关闭");
            home_dialog.click();
            time += ChatClient.overtime_3;
        }
        UiObject home_wifi = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/close_wifi"));
        if (home_wifi != null && home_wifi.exists()) {
            showMsg(">>>>>关闭免流量升级弹窗");
            home_wifi.click();
            time += ChatClient.overtime_3;
        }
        UiObject home_pop = mDevice.findObject(new UiSelector().resourceId("com.pingan.paces.ccms:id/ad_popupwindow"));
        if (home_pop != null && home_pop.exists()) {
            showMsg(">>>>>弹出广告");
            UiObject close_pop = home_pop.getChild(new UiSelector().className("android.widget.ImageView").index(0));
            if (close_pop.exists()) {
                showMsg(">>>>>弹出广告层关闭");
                close_pop.click();
                time += ChatClient.overtime_3;
            }
        }
        return time;
    }

    /**
     * 进入登录页面补充用户信息
     */
    private void Login() {
        showMsg(">>>>>Login");
        CCBMethodName = "Login";
        sleep(2000);
        ChatClient.addWorkTime(ChatClient.overtime_10);
        try {
            while (true) {
                sleep(ChatClient.overtime_1);
                UiObject pwd1 = CodeUtils.getUiObject_textOrClass("登录密码", "android.view.View");
                if (pwd1.exists()) {
                    showMsg(">>>>>点击密码输入1");
                    pwd1.click();
                    sleep(ChatClient.overtime_2);
                    //截取键盘模拟用户输入登录密码
                    if (user.getPwd() != null) {
                        tableType = "abc";
                        setClick(user.getPwd());
                    }
                    break;
                }
            }

            mDevice.click(50, 300);
            sleep(ChatClient.overtime_1);
            //            while (true) {
            //                UiObject mCB = mDevice.findObject(new UiSelector().resourceId("changeCheckItem"));
            //                if (mCB.exists()) {
            //                    showMsg("勾选用户条款...");
            //                    mCB.click();
            //                    sleep(ChatClient.overtime_1);
            //                    break;
            //                }
            //            }

            while (true) {
                UiObject login = mDevice.findObject(new UiSelector().resourceId("submit"));
                if (login.exists()) {
                    showMsg("点击登录按钮...");
                    login.click();
                    break;
                }
            }
            sleep(ChatClient.overtime_3);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_10, "登录方式弹出超时...", Constants.notReport);
                if (outTime) {
                    break;
                }
                UiObject ssmm = CodeUtils.getUiObject_textOrClass("跳过", "android.widget.TextView");
                UiObject button = CodeUtils.getUiObject_textOrClass("我知道了", "android.widget.Button");
                if (ssmm.exists()) {
                    ssmm.click();
                    sleep(ChatClient.overtime_3);
                } else if (button.exists()) {
                    button.click();
                    sleep(ChatClient.overtime_3);
                    break;
                }
            }

            //超时点击登录
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, "超时点击登录...", Constants.notReport);
                if (outTime) {
                    mDevice.click(90, 280);
                    mDevice.click(90, 280);
                    break;
                }
                UiObject zhzz1 = CodeUtils.getUiObject_textOrClass("银行账号转账", "android.view.View");
                if (zhzz1.exists()) {
                    showMsg(">>>>>回到转账页面，登陆成功");
                    break;
                }
            }
            ChatClient.loginS();
            setRemittance();
        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 登录成功后进入转账
     */
    private void setRemittance() {
        showMsg(">>>>>Inter");
        try {
            sleep(ChatClient.overtime_3);
            CCBMethodName = "setTimerun";
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "未找到...", Constants.notReport);
                if (outTime) {
                    break;
                }
                UiObject zhzz = CodeUtils.getUiObject_textOrClass("银行账号转账", "android.view.View");
                showMsg(">>>>>while");
                if (zhzz.exists()) {
                    showMsg(">>>>>跳转转账页面zhzz1.exists");
                    zhzz.click();
                    break;
                }
            }
            addWorkTime(ChatClient.overtime_40);
            handlePageStuck();
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "引导弹窗超时", Constants.notReport);
                if (outTime) {
                    break;
                }
                UiObject mBT = mDevice.findObject(new UiSelector().descriptionContains("我知道了").className("android.widget.Button"));
                if (mBT.exists()) {
                    showMsg(">>>>>关闭引导弹窗");
                    mBT.click();
                    break;
                }
            }

            while (true) {
                UiObject nameInput = mDevice.findObject(new UiSelector().resourceId("nameInput"));
                showMsg("====查找收款人输入框====");
                if (nameInput.exists()) {
                    showMsg("====找到收款人输入框====");
                    break;
                }
            }

            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_10, "余额查找失败...", Constants.notReport);
                if (outTime) {
                    break;
                }
                String sum = "";
                List<UiObject2> u2s = mDevice.findObjects(By.descContains("可用余额"));
                if (u2s.size() == 0) {
                    u2s = mDevice.findObjects(By.textContains("可用余额"));
                    if (u2s.size() > 0 && u2s.get(0) != null) {
                        sum = u2s.get(0).getText();
                    }
                } else {
                    if (u2s.get(0) != null) {
                        sum = u2s.get(0).getContentDescription();
                    }
                }

                if (!TextUtils.isEmpty(sum)) {
                    showMsg("sum = " + sum);
                    sum = sum.substring(sum.indexOf("：") + 1, sum.length() - 1);
                    showMsg("sum = " + sum);
                    sumMoney = sum.replace(",", "");
                    showMsg("sum = " + sum);
                    C_updateBalance_W(Double.parseDouble(sumMoney));
                    break;
                }
                sleep(ChatClient.overtime_1);
            }
            while (true) {
                sleep(ChatClient.overtime_3);
                RUNING = list.size() > 0;
                if (RUNING) {
                    orderBankName = Constants.getBank(list.get(0).getBankName(), BANK_FLAG);
                    if (ConfirmOrder) {
                        //查账
                        showMsg("开始查账");
                        auditOrder();
                    } else {//转账
                        showMsg("开始转账");
                        remittanceLogin();
                    }
                } else {
                    if (PAUSE) {
                        showMsg("进入等待订单流程");
                        setTimerun();
                    } else {
                        S_paused_C();
                        sleep(ChatClient.overtime_1);
                    }
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }

    }


    /**
     * 转账汇款
     */
    private void remittanceLogin() {
        try {
            CCBMethodName = "remittanceLongin";
            handlePageStuck();
            sleep(ChatClient.overtime_2);
            while (true) {
                UiObject bankNo_;
                bankNo_ = mDevice.findObject(new UiSelector().resourceId("inputCardNum"));
                if (bankNo_.exists()) {
                    showMsg("=====填充银行卡号====");
                    bankNo_.setText(list.get(0).getBankNo());
                    sleep(ChatClient.overtime_1);
                    mDevice.click(500, 260);
                    break;
                }
            }
            sleep(ChatClient.overtime_3);
            getSumMoney(true);
            while (true) {
                UiObject nameInput;
                nameInput = mDevice.findObject(new UiSelector().resourceId("nameInput"));
                if (nameInput.exists()) {
                    nameInput.click();
                    showMsg("=====填充姓名====");
                    nameInput.setText(list.get(0).getUserName());
                    sleep(ChatClient.overtime_1);
                    mDevice.click(500, 260);
                    break;
                }
            }

            sleep(ChatClient.overtime_3);
            clickMoney();
            sleep(ChatClient.overtime_2);
            /*选择银行名*/
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_10, "未找到银行...", Constants.notReport);
                if (outTime) {
                    notBankName(false);
                    return;
                }
                UiObject mTV_bank_name_ = CodeUtils.getUiObject_textOrIndex("开户行", 0);
                if (mTV_bank_name_.exists()) {
                    UiObject mV_bank_name_search = mTV_bank_name_.getFromParent(new UiSelector().index(2).className("android.view.View"));
                    if (mV_bank_name_search.exists()) {
                        String text = CodeUtils.getUiObject_content(mV_bank_name_search);
                        showMsg("找到银行名---" + text);
                        if (text.contains("请选择")) {
                            showMsg("=====进入开户行选择页面====");
                            mV_bank_name_search.click();
                            sleep(ChatClient.overtime_5);
                            if (selectBankName()) {
                                showMsg("银行选择成功...");
                                break;
                            } else {
                                notBankName(true);
                                return;
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
            sleep(ChatClient.overtime_2);
            if (sumMoney.equals("")) {
                sumMoney = "-1";
            }
            showMsg("===moeny====:" + sumMoney);
            //账户余额是否大于转账金额
            if (Double.parseDouble(sumMoney) < Double.parseDouble(list.get(0).getMoney())) {
                failureToReport("账户余额不足", 1, 0);
                return;
            }
            //            mDevice.drag(135, 1400, 135, 800, 30);
            sleep(ChatClient.overtime_3);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "下一步超时...", Constants.notReport);
                if (outTime) {
                    if (screenHeight != 2102) {
                        mDevice.click(540, 1980);
                    } else {
                        mDevice.click(360, 1910);
                    }
                    break;
                }
                UiObject ok = CodeUtils.getUiObject_textOrClass("下一步", "android.widget.Button");
                if (ok.exists()) {
                    showMsg(">>>>>进入下一步流程点击完成——————"+android.os.Build.MODEL);
                    switch (android.os.Build.MODEL) {
                        case "GOME 2017M27A":
                            mDevice.click(540, 1900);
                            break;
                        case "Lenovo L38041":
                            mDevice.click(540, 1910);
                            break;
                        case "Lenovo K520":
                            mDevice.click(540, 1910);
                            break;
                    }
                    CCBMethodName = "Submit";//提交
                    sleep(ChatClient.overtime_2);
                    break;
                }
            }
            showMsg(">>>>>进入下一步流程");
            while (true) {
                sleep(ChatClient.overtime_1);
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_8, "检测是否有错误弹框超时", Constants.notReport);
                if (outTime) {
                    break;
                }
                showMsg("=====检测是否有错误弹框====");
                UiObject view = mDevice.findObject(new UiSelector().className("android.view.View").resourceId("superTransfer"));
                UiObject view1 = view.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject view2 = view1.getChild(new UiSelector().className("android.view.View").index(16));
                UiObject view3 = view2.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject view4 = view3.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject next = mDevice.findObject(new UiSelector().text("我知道了").className("android.widget.Button"));
                UiObject nextdesc = mDevice.findObject(new UiSelector().description("我知道了").className("android.widget.Button"));
                if (next.exists()) {
                    String info = view4.getText();
                    showMsg("=====点击按钮取消弹框====" + info);
                    next.click();
                    if (isCheckStr(info)) {
                        failureToReport(info, 0, 0);
                    } else {
                        failureToReport(info, 6, 0);
                    }
                    return;
                }
                if (nextdesc.exists()) {
                    List<UiObject2> u2s = mDevice.findObjects(By.clazz("android.webkit.WebView"));
                    List<UiObject2> u2ss = u2s.get(0).getChildren().get(0).getChildren();
                    String msg = u2ss.get(u2ss.size() - 2).getContentDescription();
                    showMsg("=====点击按钮取消弹框====" + msg);
                    nextdesc.click();
                    if (isCheckStr(msg)) {
                        failureToReport(msg, 0, 0);
                    } else {
                        failureToReport(msg, 6, 0);
                    }
                    sleep(ChatClient.overtime_1);
                    back();
                    sleep(ChatClient.overtime_3);
                    setRemittance();
                }
            }
            //密码框弹起前
            CCBMethodName = "Re_pwd_S";
            sleep(1000);
            while (true) {
                UiObject next1 = CodeUtils.getUiObject_textOrClass("继续转账", "android.widget.Button");
                UiObject next3 = CodeUtils.getUiObject_textOrClass("继续转账 ", "android.widget.Button");
                UiObject SPwd1 = CodeUtils.getUiObject_textOrClass("忘记密码?", "android.view.View");
                if (next1.exists()) {
                    next1.click();
                } else if (next3.exists()) {
                    next3.click();
                } else if (SPwd1.exists()) {
                    showMsg(">>>>>进入输入密码流程");
                    sleep(ChatClient.overtime_1);
                    setClickN(user.getBankpwd());
                    showMsg(">>>>>输入密码流程完毕");
                    //密码框弹起后
                    CCBMethodName = "Re_pwd_E";
                    break;
                }
            }
            Constants.CODE = "";
            //判断是否获取到短信
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, LogToFile.getMethodName(), Constants.isReport);
                if (outTime) {
                    return;
                }
                UiObject text_code1 = CodeUtils.getUiObject_textOrClass("收不到验证码?", "android.view.View");
                if (text_code1.exists()) {
                    //短信验证码框弹起前
                    CCBMethodName = "Re_send_S";
                    showMsg(">>>>>进入短信验证码输入流程");
                    break;
                }
                UiObject mBT_next = CodeUtils.getUiObject_textOrClass("继续转账", "android.widget.Button");
                UiObject mTV_resultPage = mDevice.findObject(new UiSelector().text("完成").className("android.widget.TextView"));
                if (mBT_next.exists() || mTV_resultPage.exists()) {
                    showMsg(">>>>>无短信验证码验证环节，直接进入结果页");
                    ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
                    getUserInfo(sumMoney);
                    return;
                }
            }
            smsFlag = 1;
            //判断是否获取到短信
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_180, "短信验证码错误...", Constants.isReport);
                if (outTime) {
                    return;
                }
                if (screenHeight != 2102) {
                    mDevice.click(840, 1020);
                    mDevice.click(840, 1020);
                } else {
                    mDevice.click(840, 1150);
                    mDevice.click(840, 1150);
                }
                String useType = user.getUseType();
                if (!useType.equals("1")) {
                    String msg = getMsgBySimulator();
                    if (!TextUtils.isEmpty(msg)) {
                        setClickN(msg);
                        if (!isMsgError()) {
                            //输入短信验证码
                            showMsg(">>>>>短信验证码输入流程完成");
                            CODE_TIMER = 0;
                            ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
                            Constants.CODE = "";
                            getUserInfo(sumMoney);
                            break;
                        } else {
                            boolean b = reCovered();
                            if (!b) {//流程无法继续，清除订单，上报失败
                                mDevice.pressBack();
                                sleep(ChatClient.overtime_3);
                                while (true) {
                                    UiObject zhzz = CodeUtils.getUiObject_textOrClass("银行账号转账", "android.view.View");
                                    showMsg(">>>>>while");
                                    if (zhzz.exists()) {
                                        showMsg(">>>>>返回转账页面");
                                        zhzz.click();
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                    }
                } else {
                    getMsgByPhone();
                    setClickN(Constants.CODE);
                    //输入短信验证码
                    showMsg(">>>>>短信验证码输入流程完成");
                    CODE_TIMER = 0;
                    ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
                    Constants.CODE = "";
                    getUserInfo(sumMoney);
                    break;
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }

    }

    private void notBankName(boolean isBankSelectPage) throws UiObjectNotFoundException {
        failureToReport(" 看，                         ", 1, 0);
        back();
        if (isBankSelectPage) {
            sleep(ChatClient.overtime_2);
            back();
        }
        sleep(ChatClient.overtime_2);
        while (true) {
            UiObject mTV = CodeUtils.getUiObject_textOrClass("银行账号转账", "android.view.View");
            if (mTV.exists()) {
                showMsg("=====点击银行账号转账====");
                mTV.click();
                return;
            }
        }
    }

    private boolean selectBankName() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_8, LogToFile.getMethodName(), Constants.notReport);
            if (outTime) {
                return false;
            }
            UiObject mET_bankName = mDevice.findObject(new UiSelector().resourceId("bankName"));
            if (mET_bankName.exists()) {
                mET_bankName.setText(list.get(0).getBankName());
                sleep(ChatClient.overtime_1);
                UiObject mTV_notBank = mDevice.findObject(new UiSelector().textContains("找不到"));
                if (mTV_notBank.exists()) {
                    showMsg("未找到银行...");
                    return false;
                }
                UiObject mV_bankName = mDevice.findObject(new UiSelector().text(orderBankName).className("android.view.View"));
                if (mV_bankName.exists()) {
                    showMsg("点击---" + orderBankName);
                    mV_bankName.click();
                    sleep(ChatClient.overtime_2);
                    return true;
                }
            }
        }
    }

    private void clickMoney() throws UiObjectNotFoundException {
        while (true) {
            UiObject money1 = CodeUtils.getUiObject_textOrClass("免手续费", "android.view.View");
            if (money1.exists()) {
                money1.click();
                sleep(ChatClient.overtime_2);
                showMsg("=====填充转账金额1====");
                setClickN(list.get(0).getMoney());
                mDevice.click(500, 260);
                sleep(ChatClient.overtime_1);
                mDevice.click(500, 260);
                matchMoney();
                break;
            }
        }
    }

    private void matchMoney() throws UiObjectNotFoundException {
        showMsg("开始匹配金额...");
        while (true) {
            UiObject mTV = CodeUtils.getUiObject_textOrClass("转账金额", "android.view.View");
            if (mTV.exists()) {
                UiObject moneyParent = mTV.getFromParent(new UiSelector().className("android.view.View").index(1));
                if (moneyParent.exists()) {
                    UiObject mET_money = moneyParent.getChild(new UiSelector().className("android.view.View").index(0));
                    if (mET_money.exists()) {
                        String moneyText = CodeUtils.getUiObject_content(mET_money);
                        if (!StringUtils.isEmpty(moneyText)) {
                            String money = moneyText.replaceAll("元", "");
                            money = money.replaceAll(",", "");
                            showMsg("money---" + money);
                            if (money.equals("免手续费")) {
                                mET_money.click();
                                sleep(ChatClient.overtime_1);
                                showMsg("无金额，排除00.00");
                                for (int i = 0; i < 5; i++) {
                                    if (screenHeight != 2102) {
                                        mDevice.click(905, 1940);
                                    } else {
                                        mDevice.click(905, 2020);
                                    }
                                    sleep(ChatClient.overtime_1);
                                }
                                mDevice.click(500, 260);
                                sleep(ChatClient.overtime_1);
                                clickMoney();
                            } else if (Double.parseDouble(money) == Double.parseDouble(list.get(0).getMoney())) {
                                showMsg("输入金额匹配");
                                break;
                            } else {
                                showMsg("输入金额不匹配,重新输入");
                                mET_money.click();
                                sleep(ChatClient.overtime_1);
                                for (int i = 0; i < moneyText.length() + 1; i++) {
                                    if (screenHeight != 2102) {
                                        mDevice.click(905, 1940);
                                    } else {
                                        mDevice.click(905, 2020);
                                    }
                                    sleep(ChatClient.overtime_1);
                                }
                                mDevice.click(500, 260);
                                sleep(ChatClient.overtime_1);
                                mDevice.click(500, 260);
                                clickMoney();
                            }
                        }
                    }
                }
            }
        }
    }

    private int smsFlag = 1;

    /**
     * @return 是否以完成转账流程，true：完成（以走到结果页），false:未完成（界面超时，短信输入错误三次以上）
     * @throws UiObjectNotFoundException 异常捕获
     */
    private boolean reCovered() {
        smsFlag++;
        if (smsFlag > 3) {
            failureToReport("验证码错误超过3次", 1, 0);
            return false;
        }
        showMsg(">>>>>错误重新获取");
        ChatClient.addWorkTime(ChatClient.overtime_30);
        CCBMethodName = "Re_send_E";
        String useType = user.getUseType();
        sleep(ChatClient.overtime_1);
        showMsg("删除已输入验证码...");
        for (int i = 0; i < 7; i++) {
            // 清楚输入的错误验证码
            if (screenHeight != 2102) {
                mDevice.click(970, 1900);
            } else {
                mDevice.click(905, 2020);
            }
            sleep(500);
        }
        Constants.CODE = "";
        if (!useType.equals("1")) {

            String msg = getMsgBySimulator();
            if (!TextUtils.isEmpty(msg)) {
                setClickN(msg);
                if (!isMsgError()) {
                    //输入短信验证码
                    showMsg(">>>>>短信验证码输入流程完成");
                    CODE_TIMER = 0;
                    ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
                    Constants.CODE = "";
                    getUserInfo(sumMoney);
                } else {
                    return reCovered();
                }
            } else {
                return reCovered();
            }
        } else {
            getMsgByPhone();
            setClickN(Constants.CODE);
            //输入短信验证码
            showMsg(">>>>>短信验证码输入流程完成");
            CODE_TIMER = 0;
            ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
            Constants.CODE = "";
            getUserInfo(sumMoney);
        }
        return true;
    }

    private boolean isMsgError() {
        sleep(ChatClient.overtime_1);
        mDevice.click(990, 1400);
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            sleep(1000);
            showMsg(">>>>>检测验证码是否正确");
            boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, LogToFile.getMethodName(), Constants.notReport);
            if (outTime) {
                return false;
            }
            List<UiObject2> u2s1 = mDevice.findObjects(By.descContains("验证码输入错误"));
            List<UiObject2> u2s2 = mDevice.findObjects(By.textContains("验证码输入错误"));
            if (u2s1.size() > 0 || u2s2.size() > 0) {
                showMsg(">>>>>检测验证码错误，重新获取");
                return true;
            }
        }
    }


    /**
     * 完成转账后回复服务端
     *
     * @param sumMoney1：余额
     */
    private void getUserInfo(String sumMoney1) {
        try {
            sleep(ChatClient.overtime_2);
            String type = "";
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            showMsg(">>>>>进入转账完成流程");
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, LogToFile.getMethodName(), Constants.isReport);
                if (outTime) {
                    break;
                }
                UiObject state1 = CodeUtils.getUiObject_textOrClass("转账提交成功", "android.view.View");
                UiObject state3 = CodeUtils.getUiObject_textOrClass("转账失败", "android.view.View");
                UiObject state5 = CodeUtils.getUiObject_textOrClass("处理中...", "android.view.View");
                if (state1.exists() || state5.exists()) {
                    sleep(ChatClient.overtime_2);
                    if (state1.exists()) {
                        type = CodeUtils.getUiObject_content(state1);
                    } else if (state5.exists()) {
                        type = CodeUtils.getUiObject_content(state5);
                    }

                    if (Constants.getStr(type) == 0) {
                        DecimalFormat formater = new DecimalFormat("#0.##");
                        sumMoney = formater.format(Double.parseDouble(sumMoney1) - Double.parseDouble(list.get(0).getMoney()));
                        showMsg("保存余额...");
                        String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                        FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                        break;
                    } else {
                        sumMoney = sumMoney1;
                        break;
                    }
                } else if (state3.exists()) {
                    showMsg(">>>>>转账失败");
                    type = "转账失败";
                    break;
                }
            }
            showMsg(">>>>>转账完成流程");
            type = type.replaceAll("\r|\n|\\s", "");
            failureToReport(type, Constants.getStr(type), 0);
            sleep(ChatClient.overtime_3);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            showMsg(">>>>>检测转账完成页的信息");
            while (true) {
                boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_15, "进入继续转账流程", Constants.notReport);
                if (outTime) {
                    break;
                }
                UiObject seven1 = CodeUtils.getUiObject_contentText("继续转账");
                UiObject finish = CodeUtils.getUiObject_textOrClass("取消", "android.widget.Button");
                if (seven1.exists()) {
                    showMsg(">>>>>进入继续转账流程1");
                    mDevice.click(100, 150);
                    mDevice.click(100, 150);
                    break;
                } else if (finish.exists()) {
                    showMsg(">>>>>点击取消1");
                    finish.click();
                }
            }
            sleep(ChatClient.overtime_3);
            while (true) {
                UiObject mET_inputCardNum = mDevice.findObject(new UiSelector().resourceId("inputCardNum"));
                if (mET_inputCardNum.exists()) {
                    showMsg("以返回转账页");
                    mDevice.drag(20, 600, 20, 1300, 30);
                    break;
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }
    }


    /**
     * 查账
     *
     * @throws UiObjectNotFoundException 异常捕获
     */
    private void auditOrder() throws UiObjectNotFoundException, RemoteException {
        addWorkTime(ChatClient.overtime_60);
        handlePageStuck();
        getSumMoney(false); // 获取金额
        sleep(ChatClient.overtime_1);
        back();// 返回
        sleep(ChatClient.overtime_2);
        //进入转账记录
        while (true) {
            UiObject remittance = CodeUtils.getUiObject_textOrClass("转账记录", "android.view.View");
            if (remittance.exists()) {
                CCBMethodName = "auditOrder";
                sleep(ChatClient.overtime_3);
                remittance.click();
                break;
            }
        }
        sleep(ChatClient.overtime_3);
        showMsg("进入《转账交易记录》页1");
        //获取交易记录
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_3, "", Constants.notReport);
            if (outTime) {
                showMsg("账单查找超时");
                failureToReport("查询超时", 1, 1);
                break;
            }
            showMsg("账单查找...");
            sleep(ChatClient.overtime_2);
            UiObject view;
            if (!android.os.Build.MODEL.contains("2017M27A")) {
                view = mDevice.findObject(new UiSelector().className("android.webkit.WebView").text("转账记录"));
            } else {
                view = mDevice.findObject(new UiSelector().className("android.webkit.WebView").descriptionContains("转账记录"));
            }
            UiObject view0 = view.getChild(new UiSelector().className("android.view.View").index(0));
            UiObject viewChild = view0.getChild(new UiSelector().className("android.view.View").index(1));
            UiObject viewChild1 = viewChild.getChild(new UiSelector().className("android.view.View").index(1));
            UiObject view1;
            UiObject view2 = null;
            UiObject view4 = null;
            view1 = viewChild1.getChild(new UiSelector().className("android.view.View").index(1));
            if (!android.os.Build.MODEL.contains("2017M27A")) {
                view2 = viewChild1.getChild(new UiSelector().className("android.view.View").index(2));
                view4 = viewChild1.getChild(new UiSelector().className("android.view.View").index(4));
            } else {
                UiObject v1 = viewChild1.getChild(new UiSelector().className("android.view.View").index(2));
                if (v1.exists()) {
                    view2 = v1.getChild(new UiSelector().className("android.view.View").index(0));
                }
                UiObject v2 = viewChild1.getChild(new UiSelector().className("android.view.View").index(4));
                if (v2.exists()) {
                    view4 = v2.getChild(new UiSelector().className("android.view.View").index(0));
                }
            }

            if (view1.exists() && view2.exists() && view4.exists()) {
                showMsg("查到条目...");
                String username = CodeUtils.getUiObject_content(view1);
                String money = CodeUtils.getUiObject_content(view2);
                String status = CodeUtils.getUiObject_content(view4);
                username = username.trim();
                String bankNo = "";
                if (!username.isEmpty()) {
                    bankNo = username.substring(username.length() - 5, username.length() - 1);
                }
                String moery1 = money.replaceAll("-", "");
                moery1 = Util.replaceMoney(moery1);
                showMsg("username：" + username + "---money：" + moery1 + "---bankNo:" + bankNo
                        .substring(bankNo.length() - 4, bankNo.length()) + "---status" + status);
                String bankNo1 = list.get(0).getBankNo();
                bankNo1 = bankNo1.substring(bankNo1.length() - 4);
                if (Double.parseDouble(list.get(0).getMoney()) == Double.parseDouble(moery1) && username.contains(list.get(0).getUserName())
                        && bankNo.equals(bankNo1) && status.contains("成功")) {
                    failureToReport("交易成功", 0, 1);
                } else {
                    String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                    String oldMoney = FileHelper.readSDFile(sumMoneyFile);
                    switch (Constants.changeInAmount(BANK_NAME, oldMoney, sumMoney, list.get(0).getMoney())) {
                        case "0":
                            failureToReport("交易成功", 0, 1);
                            break;
                        case "1":
                            failureToReport("交易待处理...", 1, 1);
                            break;
                        case "4":
                            failureToReport("未查到条目", 4, 1);
                            break;
                    }
                }
                break;
            }
        }

        //返回
        back();
        while (true) {
            UiObject zhzz = CodeUtils.getUiObject_textOrClass("银行账号转账", "android.view.View");
            if (zhzz.exists()) {
                zhzz.click();
                break;
            }
        }
    }

    /**
     * 等待账单
     *
     * @throws UiObjectNotFoundException 异常捕获
     * @throws RemoteException           异常捕获
     */
    private void setTimerun() throws UiObjectNotFoundException, RemoteException {
        CCBMethodName = "setTimerun";
        sleep(ChatClient.overtime_3);
        handlePageStuck();
        getSumMoney(true); // 获取金额
        sleep(ChatClient.overtime_1);
        if (!ChatClient.waitExistOrder(ChatClient.overtime_60)) {
            back();// 返回
            sleep(ChatClient.overtime_3);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                sleep(ChatClient.overtime_1);
                UiObject zhzz = CodeUtils.getUiObject_textOrClass("银行账号转账", "android.view.View");
                showMsg(">>>>>检测银行账号转账");
                if (zhzz.exists()) {
                    zhzz.click();
                    break;
                }
            }
        }
        Constants.CODE = "";
    }

    /**
     * 界面卡死转圈解决
     *
     * @throws RemoteException           异常捕获
     * @throws UiObjectNotFoundException 异常捕获
     */
    private void handlePageStuck() throws RemoteException, UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, "平安转账界面卡死点击运行应用键", Constants.notReport);
            if (outTime) {
                showMsg("=====平安转账界面卡死点击运行应用键=====");
                mDevice.pressRecentApps();
                sleep(2000);
                while (true) {
                    UiObject mTV_app_name = null;
                    if (android.os.Build.MODEL.contains("2017M27A")) {
                        UiObject mTV = mDevice.findObject(new UiSelector().resourceId("com.android.systemui:id/app_name").text("平安口袋银行"));
                        if (mTV.exists()) {
                            mTV_app_name = mTV.getFromParent(new UiSelector().resourceId("com.android.systemui:id/cardview").index(1));
                        }
                    } else {
                        mTV_app_name = mDevice.findObject(new UiSelector().resourceId("com.android.systemui:id/title").text("平安口袋银行"));
                    }
                    if (mTV_app_name != null && mTV_app_name.exists()) {
                        showMsg("=====平安转账界面卡死点击底部从新打开平安银行=====");
                        mTV_app_name.click();
                        break;
                    }
                }
                sleep(ChatClient.overtime_5);
            }
            sleep(ChatClient.overtime_1);
            UiObject mV = mDevice.findObject(new UiSelector().text("银行账号转账").className("android.view.View"));
            if (mV.exists()) {
                mV.click();
                sleep(ChatClient.overtime_3);
            }
            UiObject nameInput = mDevice.findObject(new UiSelector().resourceId("nameInput"));
            if (nameInput.exists()) {
                break;
            }
        }
    }

    /**
     * 字母和数字键盘的切换
     *
     * @param checkStr 字符数字
     * @return true为数字，false为字母
     */
    public static boolean isNumeric(String checkStr) {
        try {
            Integer.parseInt(checkStr);
            return true;
        } catch (NumberFormatException err) {
            return false;
        }
    }

    /**
     * 模拟点击密码
     *
     * @param pad_ 密码
     */
    private void setClick(String pad_) {
        try {
            char[] abc = "qwertyuiopasdfghjklzxcvbnm".toCharArray();//char数组
            char[] num = "1234567890".toCharArray();//char数组
            char[] pwd = pad_.toCharArray();//char数组
            char[] num_abc;//char数组
            for (char c : pwd) {
                if (isNumeric(String.valueOf(c))) {
                    if (tableType.equals("abc")) {
                        mDevice.click(tableAbc123[0], tableAbc123[1]);
                        tableType = "123";
                        sleep(ChatClient.overtime_1);
                    }
                    num_abc = num;
                } else {
                    if (tableType.equals("123")) {
                        mDevice.click(tableAbc123[0], tableAbc123[1]);
                        tableType = "abc";
                        sleep(1000);
                    }
                    num_abc = abc;
                }
                for (int i = 0; i < num_abc.length; i++) {
                    if (num_abc[i] == c) {
                        if (isNumeric(String.valueOf(c))) {
                            mDevice.click(xy_123[i][0], xy_123[i][1]);
                            sleep(1000);
                            Log.d("====---点击数字====" + c, xy_123[i][0] + "，" + xy_123[i][1]);
                        } else {
                            mDevice.click(xy_abc[i][0], xy_abc[i][1]);
                            sleep(1000);
                            Log.d("====---点击字母====" + c, xy_abc[i][0] + "，" + xy_abc[i][1]);
                        }
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 模拟点击数字流程
     *
     * @param num 金额
     */
    private void setClickN(String num) {
        showMsg(">>>>>进入点击数字流程");
        char[] n = "123456789.0".toCharArray();//char数组
        char[] jine = num.toCharArray();//char数组
        if (jine.length < 1) {
            return;
        }
        for (int j = 0; j < jine.length; j++) {
            for (int i = 0; i < n.length; i++) {
                if (n[i] == jine[j]) {
                    mDevice.click(xy_num_m[i][0], xy_num_m[i][1]);
                    showMsg("====---点击数字====" + j);
                    sleep(1000);
                    break;
                }
            }
        }
    }

    /**
     * @param info             上报内容
     * @param state            上报状态
     * @param auditAndTransfer 0为转账；1为查账
     */
    private static void failureToReport(String info, int state, int auditAndTransfer) {
        String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'" + BANK_NAME +
                "'," + "'info':'" + info + "'";
        String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':" + state + "," +
                "'type':1";
        showLog("mobileD = " + mobileD);
        showLog("json = " + json);
        String requestName;
        if (auditAndTransfer == 0) {
            requestName = ChatClient.M_SEND_TRADE_RESULT_P;
        } else {
            requestName = ChatClient.S_CONFIRM_TRADE_RESULT_C;
        }
        ChatClient.msg("<YZF>{'header':'" + requestName + "'," + json + "," + mobileD + "}</YZF>");
        //删除处理过的订单
        mapDelete(list.get(0).getOrder());
        getmap();
        sleep(ChatClient.overtime_2);
    }

    /**
     * 检测银行卡和姓名是否匹配
     */
    private static boolean isCheckStr(String info) {
        boolean b = false;
        if (info.contains("请输入正确的收款人姓名") || info.contains("请选择所属银行") || info.contains("请选择支行名称")) {
            b = true;
        }
        return b;
    }

    //真机短信验证码
    private void getMsgByPhone() {
        //判断是否获取到短信
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean outTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, LogToFile.getMethodName(), Constants.isReport);
            if (outTime) {
                return;
            }

            while (Constants.CODE.equals("")) {
                showMsg(">>>>>获取短信验证码");
                sleep(ChatClient.overtime_1);
            }
            break;
        }
    }

    private String getMsgBySimulator() {
        if (smsFlag > 1) {
            sleep(ChatClient.overtime_1);
        } else {
            sleep(ChatClient.overtime_5);
        }
        ChatClient.requestSMSCode(user.getUserName(), user.getBankId());
        //判断是否获取到短信
        long longTime = System.currentTimeMillis();//开始时间
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean outTime = CodeUtils.outTime(longTime, ChatClient.overtime_60, "未收到短信...", Constants.notReport);
            if (outTime) {
                return "";
            }
            sleep(1000);
            if (!TextUtils.isEmpty(ChatClient.smsCode.getSmscode()) && ChatClient.smsCode.getTelephone().equals(user
                    .getUserName())) {
                showMsg("收到短信验证码：" + ChatClient.smsCode.getSmscode());
                return ChatClient.smsCode.getSmscode();
            } else {
                String str = "未收到验证码，收到短信验证码手机号不匹配";
                showMsg(str + ChatClient.smsCode.getTelephone());
                return "";
            }
        }
    }

    /**
     * 获取可用金额
     *
     * @param isSave 除查账外其他时间查账都要保存为保证余额的实时性 true:保存到本地 false：不保存
     */

    private void getSumMoney(boolean isSave) {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_10) {
                break;
            }
            String sum = "";
            List<UiObject2> u2s = mDevice.findObjects(By.descContains("可用余额"));
            if (u2s.size() == 0) {
                u2s = mDevice.findObjects(By.textContains("可用余额"));
                if (u2s.size() > 0 && u2s.get(0) != null) {
                    sum = u2s.get(0).getText();
                }
            } else {
                if (u2s.get(0) != null) {
                    sum = u2s.get(0).getContentDescription();
                }
            }

            if (!TextUtils.isEmpty(sum)) {
                showMsg("sum = " + sum);
                sum = sum.substring(sum.indexOf("：") + 1, sum.length() - 1);
                showMsg("sum = " + sum);
                sumMoney = sum.replace(",", "");
                showMsg("sum = " + sum);
                if (isSave) {
                    showMsg("本地保存余额");
                    String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                    FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                }
                C_updateBalance_W(Double.parseDouble(sumMoney));
                break;
            }
            sleep(1000);
        }
    }

    /**
     * 返回
     */
    private void back() {
        showMsg(">>>>>返回上页");
        mDevice.pressBack();
    }

    private void showMsg(String msg) {
        LogToFile.showLog(BANK_NAME, msg);
    }
}
